package com.AdvancedDemos.Stream流操作;

import java.util.ArrayList;
import java.util.stream.Stream;

public class Stream流终结方法 {
    public static void main(String[] args) {
        //Stream流对象,调用方法返回的不是Stream对象,表示该方法是终结方法
//        //1. void forEach(Consumer<? super T> action) 打印流中剩余元素
        ArrayList<Teacher> list = new ArrayList<>();
        list.add(new Teacher("张三1", 23));
        list.add(new Teacher("张三2", 21));
        list.add(new Teacher("张三4", 18));
        list.add(new Teacher("张三3", 25));
        Stream<Teacher> stream = list.stream();
//        stream.forEach(new Consumer<Teacher>() {
//            @Override
//            public void accept(Teacher teacher) {
//                System.out.println(teacher);
//            }
//        });
//        //Teacher{name='张三1', age=23}
//        //Teacher{name='张三2', age=21}
//        //Teacher{name='张三4', age=18}
//        //Teacher{name='张三3', age=25}


//        //2. long count 统计流中剩余元素的个数
//        long count = stream.count();
//        System.out.println(count);//4


//        //3. Optional<T> max(Comparator<? super T>comparator) --- 根据比较器排序规则获取流中最大的元素
//        Optional<Teacher> max = stream.max(new Comparator<Teacher>() {
//            @Override
//            public int compare(Teacher o1, Teacher o2) {
//                return o2.getAge() - o1.getAge();//按照年龄逆序排序
////                return o1.getAge() - o2.getAge();//按照年龄升序排序
//            }
//        });
//        System.out.println(max);//Optional[Teacher{name='张三4', age=18}]  直接打印max打印的是排序后最后面一个元素
//        Teacher maxAgeTeacher = max.get();
//        System.out.println(maxAgeTeacher);//Teacher{name='张三4', age=18}  按照降序排序,默认打印最后面一个元素,所以成了最小值


//        //4. Optional<T> min(Comparator<? super T>comparator) --- 根据比较器排序规则获取流中最小的元素
//        Optional<Teacher> min = stream.min(new Comparator<Teacher>() {
//            @Override
//            public int compare(Teacher o1, Teacher o2) {
//                return o2.getName().compareTo(o1.getName());//字符串逆序自然排序
////                return o1.getName().compareTo(o2.getName());//字符串顺序自然排序
//            }
//        });
//        System.out.println(min);//Optional[Teacher{name='张三4', age=18}]  直接打印min打印的是排序后最前面一个元素
//        Teacher minNameTeacher = min.get();
//        System.out.println(minNameTeacher);//Teacher{name='张三4', age=18}  按照升序排序,默认打印最前面一个元素,所以成了最大值


    }
}

class Teacher {
    private String name;
    private int age;

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Teacher() {
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
